//////////////////////////////
// serial buffer loading

const char _hashcmd='*';
const char _startcmd='#';
const char _endcmd='$';

const int _iMaxSerialBuffer = 512;
const int _iMaxSerialHash = 8;
int _iSerialHashLen=0, _iSerialBufferLen=0;
char _SerialBuffer[_iMaxSerialBuffer];
char _SerialBufferHash[_iMaxSerialHash];
int _iReadMode = 0;

boolean readWireString (String &cmd) {
  int sb;

  while (Wire.available())
  {
    sb = Wire.read();
    Serial.println((char)sb);
    
    if(sb==_hashcmd) // header/hash ?
    {
      _iReadMode=1;
      _iSerialHashLen=0;
      continue;
    }
    else if(sb==_startcmd) // start cmd ?
    {
      _iReadMode=2;
      _SerialBufferHash[_iSerialHashLen] = '\0';
      _iSerialBufferLen=0;
      setMatrixAnimation(LED_ANIM_ECHO);
      continue;
    }
    else if(sb==_endcmd) // end cmd ?
    {
      if(_iReadMode!=2)
      {
        _iReadMode=0; 
        continue;
      }
      _iReadMode=0;

      if(_iSerialHashLen > 0)
      {
        // verify hash !
        String sCmdHash = _SerialBufferHash;
        byte iCmdHash = sCmdHash.toInt();

        byte iHash=0;
        for(int t=0; t<_iSerialBufferLen; t++) iHash += (byte)(_SerialBuffer[t]);
        if(iHash == iCmdHash)
        {
          _SerialBuffer[_iSerialBufferLen] = '\0';
          cmd = _SerialBuffer;
          return true;
        }
      }
      else
      {
        _SerialBuffer[_iSerialBufferLen] = '\0';
        cmd = _SerialBuffer;
        return true;
      }
      return false;
    }

    if(_iReadMode==1)
    {
      if(_iSerialHashLen>=_iMaxSerialHash)
      {
        _iReadMode = 0;
        continue;
      }

      _SerialBufferHash[_iSerialHashLen]=sb;
      _iSerialHashLen++;
    }
    else if(_iReadMode==2)
    {
      if(_iSerialBufferLen>=_iMaxSerialBuffer)
      {
        _iReadMode = 0;
        continue;
      }

      _SerialBuffer[_iSerialBufferLen] = sb;
      _iSerialBufferLen++;
    }
  }

  return false;
}

///////////////////////////////
// check commands sended
int load_int(String &s, const int start, int &iRet)
{
  int iLen = start+1;
  String num="";

  if (s[start] == '-') num += "-";
  else if(s[start] == '+') num += "";
  else return start;

  for(int i=1; i<6 && s.length()>(start+i); i++)
  {
    char c = s[start+i];
    if(!isDigit(c))
      break;
    num += c;
    iLen ++;
  }

  iRet = num.toInt();
  return iLen;
}
int load_int_nosigne(String &s, const int start, int &iRet)
{
  int iLen = start;
  String num="";

  if (s[start] == '-') num += "-";
  else if(s[start] == '+') num += "";

  for(int i=0; i<6 && s.length()>(start+i); i++)
  {
    char c = s[start+i];
    if(!isDigit(c))
      break;
    num += c;
    iLen ++;
  }

  iRet = num.toInt();
  return iLen;
}

boolean check_commands(void)
{
  String line="";
  boolean bReturn = false;
  while(readWireString(line))
  {
    if(line.length()==0) continue;

    switch(line[0])
    {
    case 'a': // (A)nimation
      {
        switch(line[1])
        {
        case 's': // (S)et
          {
            // #as+2$
            int next=2;
            int anim=0;
            next = load_int(line, next, anim);
            setMatrixAnimation(anim);
          }
          break;
        }
      }
      break;
    }
    bReturn = true;
  }
  return bReturn;
}





